//
//  HomepageRootController.m
//  collection
//
//  Created by 靖 张 on 11/22/11.
//  Copyright (c) 2011 Tsinghua University. All rights reserved.
//

#import "HomepageRootController.h"
#import "LatestInfoController.h"
#import "TagInfoController.h"
#import "DiscoveryInfoController.h"
#import "AppDelegate.h"

@interface HomepageRootController()

- (IBAction)onClickTabButton:(id)sender;

@end

@implementation HomepageRootController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        // Sub tab controller
        LatestInfoController* latestInfoController = [[LatestInfoController alloc] initWithNibName:@"LatestInfoController" bundle:nil];
        latestInfoController.homepageRootController = self;
        TagInfoController* tagInfoController = [[TagInfoController alloc] initWithNibName:@"TagInfoController" bundle:nil];
        tagInfoController.homepageRootController = self;
        DiscoveryInfoController* discoveryController = [[DiscoveryInfoController alloc] initWithNibName:@"DiscoveryInfoController" bundle:nil];
        discoveryController.homepageRootController = self;
        
        _subTabControllers = [[NSArray alloc] initWithObjects:latestInfoController, tagInfoController, discoveryController, nil];
        
        [latestInfoController release];
        [tagInfoController release];
        [discoveryController release];
    }
    
    return self;
}

- (void)dealloc {
    
    [_subTabControllers release];
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)loadView {
    
    [super loadView];

    // Add the sub tab view
    UIView* controllerContainerView = [self.view viewWithTag:51];
    for (int i = 0; i < [_subTabControllers count]; i++) {
        UIViewController* subTabController = [_subTabControllers objectAtIndex:i];
        [controllerContainerView addSubview:subTabController.view];
    }
    
    // Set active view to "LatestInfo"
    [self setActiveSubTab:0];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view om its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Public methods

- (void)setActiveSubTab:(int)index {
    if (index < 0 || index > InfoType_Discovery) {
        return;
    }
    
    // Update the content view
    for (int i = 0; i < [_subTabControllers count]; i++) {
        UIViewController* subTabController = [_subTabControllers objectAtIndex:i];
        
        if (index == i) {
            subTabController.view.hidden = NO;
            self.title  = subTabController.title;
        }
        else {
            subTabController.view.hidden = YES;
        }
    }
    
    // Update the tab
    UIView* tabContainerView = [self.view viewWithTag:50];
    for (int i = 0; i < [_subTabControllers count]; i++) {
        int tag = i + 1;
        UIButton* button = (UIButton*)[tabContainerView viewWithTag:tag];
        if (button == nil) {
            continue;
        }
        
        button.selected = (index == i);
    }
}

#pragma mark - Private methods

- (IBAction)onClickTabButton:(id)sender {
    
    UIButton* button = (UIButton*)sender;
    int index = button.tag - 1;
    if (index == NSNotFound) {
        return;
    }
    
    [self setActiveSubTab:index];
}

@end
